package com.medical.sjq.controller;

import com.medical.config.SystemContext;
import com.medical.entity.SysUser;
import com.medical.service.RoleService;
import com.medical.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class LoginController {
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	
	@RequestMapping("/login")
	public String login(Integer userId, String userPassword,HttpServletRequest request, Model model) {
		
		SysUser sysUser = userService.findUserByUserId(userId);
		
		if(sysUser != null && sysUser.getUserPassword() != null) {
			// 判断用户密码是否正确
		    boolean isLogin = SystemContext.passwdDecryption(userPassword, sysUser.getUserPassword());
		    if(isLogin) {
		    	// 将用户信息存入session
				request.getSession().setAttribute("userInfo", sysUser);
				// 将用户拥有菜单权限列表存入session
				List<String> listRight = roleService.getUserPermissions(sysUser.getUserId()+"");
				request.getSession().setAttribute("listRight", listRight);
				
				model.addAttribute(sysUser);
				
				return "main";
		    }
		}
		
		return "../../index";
	}
	
	@RequestMapping("/openWelcomePage")
	public String openWelcomePage(Model model) {
		return "welcome";
	}
}
